package cn.jinjimi.app.main.activity;

import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.ThemeActivateEvent;
import cn.jinjimi.app.controller.event.ThemePreViewEvent;
import cn.jinjimi.app.entity.ThemePreview;
import cn.jinjimi.app.main.dialog.ThemeSuccDialog;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by haner on 15-2-6.
 */
public class ActivateThemeActivity extends ToolBarCommonActivity {

    private TextView activateTheme;
    private String themeName;
    private String themeFileName;
    private int isVip;
    private boolean used;
    private String themeId;
    private String comment;
    private ThemePreview themePreview = null;
    private LinearLayout parent;
    private LayoutInflater mInflater;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_activate_theme);
        themeName = getIntent().getStringExtra("themeName");
        themeFileName = getIntent().getStringExtra("themeFileName");
        isVip = getIntent().getIntExtra("isVip", 0);
        used = getIntent().getBooleanExtra("used", false);
        themeId = getIntent().getStringExtra("themeId");
        comment = getIntent().getStringExtra("comment");
        mInflater = LayoutInflater.from(this);
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    private void initView() {
        super.setTitle(R.string.site_theme_preview);

        TextView themeTitle = (TextView) findViewById(R.id.theme_title);
        TextView themeTag = (TextView) findViewById(R.id.theme_tag);
        TextView themeDesc = (TextView) findViewById(R.id.theme_desc);

        themeTitle.setText(themeName);
        if (isVip == 1) {
            themeTag.setText("诚信通专属");
            themeTag.setTextColor(ContextCompat.getColor(ActivateThemeActivity.this, R.color.orange));
            themeTag.setBackgroundResource(R.drawable.theme_txt_shape_vip);
        }
        themeDesc.setText(comment);
        activateTheme = (TextView) findViewById(R.id.theme_activate);
        parent = (LinearLayout) findViewById(R.id.ll_parent);

        if (used) {
            changButtomStatus();
        }
    }

    private void initListener() {
        if (!used) {
            activateTheme.setOnClickListener(this);
        }
    }

    private void initData() {
        getWebData();
    }

    private void reSetImageViewWidth(final View view) {
        //当测量完了回调方法进行重新设置宽度
        ViewTreeObserver vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                int height = view.getHeight();
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                layoutParams.width = height * 35 / 62;
                view.setLayoutParams(layoutParams);
                parent.requestLayout();
            }
        });

    }

    private void changButtomStatus() {
        activateTheme.setClickable(false);
        activateTheme.setText("已启用");
        activateTheme.setTextColor(ContextCompat.getColor(ActivateThemeActivity.this, R.color.text_middle_black));
        activateTheme.setBackgroundResource(R.drawable.selector_button_grey);
    }

    private void getWebData() {
        showLoadingIndicator();
        UserController.getThemePreview(themeFileName);
    }

    private void setData() {
        for (int i = 0; i < themePreview.getPreviews().size(); i++) {
            View view = mInflater.inflate(R.layout.item_theme_preview, parent, false);
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_preview);
            parent.addView(view);
            reSetImageViewWidth(view);
            imageView.setImageURI(Uri.parse(themePreview.getPreviews().get(i).getUrl()));
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (checkVip()) {
            activateTheme();
        }
    }

    private boolean checkVip() {
        if (isVip == 0) {
            return true;
        } else if (AccountManager.getInstance().getAccount().isVip.equals("1")) {
            return true;
        } else {
            ToastUtils.toastMessage(ActivateThemeActivity.this, "亲，只有诚信通客户才能启用此主题哦");
            return false;
        }
    }

    //启用主题
    private void activateTheme() {
        showLoadingIndicator();
        UserController.activateTheme(themeId);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onThemeActivateEvent(ThemeActivateEvent event) {
        hideLoadingIndicator();
        if (isFinishing())
            return;
        switch (event.state) {
            case DataEvent.SUCC:
                ThemeSuccDialog themeSuccDialog = new ThemeSuccDialog(ActivateThemeActivity.this);
                themeSuccDialog.setCanceledOnTouchOutside(false);
                themeSuccDialog.show();
                changButtomStatus();
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(ActivateThemeActivity.this, "启用失败，请稍后重试");
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onThemePreViewEvent(ThemePreViewEvent event) {
        if (isFinishing())
            return;
        switch (event.state) {
            case DataEvent.SUCC:
                themePreview = event.data;
                setData();
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(ActivateThemeActivity.this, "获取预览图失败，清稍后重试");
                break;
        }
        hideLoadingIndicator();
    }

    public void finishThis() {
        setResult(200);
        finish();
    }
}
